import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;



public class ChooseSeatsFrame extends JFrame{
    JLabel title = new JLabel( "Please choose a seat" );
    static int seatChosen = 0;
    static int bookingID = 0;
    static Random random = new Random();
    static int showID = 0;

    ChooseSeatsFrame( String hyperlink, int filmID, int filmTime ){
        Image bGround = ReadImage.getImage( "http://wallpapers.wallbase.cc/rozne/wallpaper-389121.jpg" );
        Browser bGPanel = new Browser( bGround );
        add(bGPanel);


        int xPos = 10;
        int yPos = 40;

        try{
            showID = Integer.parseInt ( DBConnection.getQuery("select showID from showings where filmID = " + filmID +" and filmtime = " + filmTime, ImageInJFrame.theConnection ) );
        }catch( Exception e ){
        }

        JButton[] seats = new JButton[92];
        String[] seatNumber = new String[]{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92"};

        LinkedList<Integer> bookedSeats = new LinkedList();
        LinkedList<String> stringBookedSeats = new LinkedList();

        try{
            bookedSeats = DBConnection.getLLIntQuery("select seatbooked from seats where screen = 1", ImageInJFrame.theConnection );
        }catch(Exception e){
        }

        for( Integer i: bookedSeats){
            String temp = String.valueOf(i);
            stringBookedSeats.add(temp);
        }

        for( String seatNum: stringBookedSeats){
            arrayIter:for( int i = 0; i < 92; i++){
                if (seatNum.equals(seatNumber[i])){
                    seatNumber[i] = null;
                    break arrayIter;
                }
            }
        }

        for( int i  = 0; i < 92; i++ ){
            if( seatNumber[i] != null ){
                seats[i] = new JButton( seatNumber[i] );
                seats[i].setBounds(xPos,yPos,48,48);
                bGPanel.add(seats[i]);
                seats[i].setActionCommand(seatNumber[i]);
                seats[i].addActionListener(new ActionListener(){
                    public void actionPerformed( ActionEvent e ){
                        String seatNumber = e.getActionCommand();
                        int userOption = JOptionPane.showConfirmDialog(null, "You want to book seat " + seatNumber );
                        ChooseSeatsFrame.seatChosen = Integer.parseInt( seatNumber );
                        if ( userOption == 0 ){
                            bookingID  = random.nextInt(100000);
                            Booking.addBooking( bookingID, showID , Integer.parseInt( seatNumber ), ImageInJFrame.loggedInName );
                            JOptionPane.showMessageDialog( null, "Your ticket has been booked" );
                        }
                    }
                });
            }

            if( yPos < 390 ){
                xPos = xPos + 50;
                if( xPos == 160 || xPos == 510 ){
                    xPos = xPos + 100;
                }
                if( xPos == 760 ){
                    xPos = 10;
                    yPos = yPos + 50;
                }
            }else{
                xPos = xPos + 50;
            }
        }

        bGPanel.add( title );
        title.setBounds(300,10,200,20);

        setVisible(true);
        setSize(800,500);
    }

}

